﻿var RestTestModel = function () {
    var self = this;

    self.restUrl = ko.observable('');
    self.result = ko.observable('');
    self.loadingData = ko.observable(false);
    self.hasRestUrl = ko.observable(false);
    self.httpMethods = ['GET', 'POST'];
    self.noXHttpMethod = '(none)';
    self.xHttpMethods = [self.noXHttpMethod, 'MERGE', 'PUT', 'PATCH', 'DELETE'];
    self.selectedHttpMethod = ko.observable('GET');
    self.selectedXHttpMethod = ko.observable(self.noXHttpMethod);
    self.currentFormDigest = ko.observable('');
    self.postData = ko.observable('');
    self.ajaxContentType = 'application/json;odata=verbose';

    self.test = function () {
        self.loadingData(true);
        self.clearResult();

        var ajaxOptions = {
            url: self.restUrl(),
            type: self.selectedHttpMethod(),
            contentType: self.ajaxContentType,
            cache: false,
            success: function (data) {
                if (data && data.d) {
                    self.formatJson(data.d);
                }
                self.loadingData(false);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                self.loadingData(false);
                self.renderError(errorThrown);
            }
        };
        if (self.selectedHttpMethod() == 'POST') {
            self.currentFormDigest(self.getCurrentFormDigestValue());
            ajaxOptions.headers = {
                'content-type': self.ajaxContentType,
                'X-HTTP-Method': (self.selectedXHttpMethod() != self.noXHttpMethod) ? self.selectedXHttpMethod() : '',
                'Accept': self.ajaxContentType,
                'X-RequestDigest': self.currentFormDigest(),
                'IF-MATCH': '*'
            };
            ajaxOptions.data = self.postData();
        }
        else {
            ajaxOptions.headers = { 'Accept': self.ajaxContentType };
        }

        $.ajax(ajaxOptions);
    };    
    self.formatJson = function (json) {
        self.result(FormatJSON(json));
    };
    self.clearResult = function () {
        self.result('');
    };
    self.renderError = function (errorThrown) {
        $('#restTestWP').jtoast({ type: 'error', title: 'Oooppps, an error has occurred!', body: errorThrown });
    };
    self.getCurrentFormDigestValue = function () {
        var formDigest = '';
        $.ajax({
            url: SP.PageContextInfo.get_webAbsoluteUrl().SPContextInfo(),
            type: 'POST',
            contentType: self.ajaxContentType,
            headers: { 'Accept': self.ajaxContentType },
            success: function (data) {
                formDigest = data.d.GetContextWebInformation.FormDigestValue;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            },
            async: false
        });
        return formDigest;
    };
    self.changeHttpMethod = function () {
        self.postData('');
        self.result('');
    };
};